/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.edu.fatec.ads.facade;

import br.edu.fatec.ads.bo.BusinessObjectEditora;
import br.edu.fatec.ads.classes.Editora;
import br.edu.fatec.ads.util.ComboBoxItem;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.hibernate.HibernateException;

/**
 *
 * @author Ubirajara
 */
public class FacadeEditora {

    private BusinessObjectEditora regraNegocio = null;
    private static FacadeEditora singleton = null;

    private FacadeEditora() {
        regraNegocio = new BusinessObjectEditora();
    }

    public static FacadeEditora getInstance(){
        if(singleton == null){
            singleton = new FacadeEditora();
        }

        return singleton;
    } 

    public List<Editora> recuperarTodasEditoras()
        throws HibernateException{
        List<Editora> editoras = Collections.EMPTY_LIST;

        regraNegocio.beginTransaction();

        try{
            editoras = regraNegocio.recuperarTodasEditoras();

            regraNegocio.commitTransaction();
        }catch(HibernateException cause){
            cause.printStackTrace();

            regraNegocio.commitTransaction();
        }

        return editoras;
    }

    public List<ComboBoxItem> recuperarEditoras(){
        List<ComboBoxItem> items = new ArrayList<ComboBoxItem>();
        List<Editora> editoras = this.recuperarTodasEditoras();

        for (Editora editora : editoras) {
            items.add(new ComboBoxItem(editora.getCodigo(), editora.getNome()));
        }

        return items;
    }

}